package com.choosefine.it.worker.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.choosefine.it.base.dto.PageDTO;
import com.choosefine.it.worker.dao.WorkerSalaryMonthMapper;
import com.choosefine.it.worker.model.WorkerSalaryMonth;
import com.choosefine.it.worker.model.vo.WorkerSalaryStatisticsVO;
import com.choosefine.it.worker.vo.WorkerSalaryMonthVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

/**
 * 11.28dzq 工资月表
 * @author Administrator
 * 			update code liaozhanggen 2016-12-29
 *
 */
@Service("workerSalaryMonthService")
public class WorkerSalaryMonthService {
    @Autowired
    WorkerSalaryMonthMapper workerSalaryMonthMapper;
    
    /**
     * 根据 当前员工及所在的班组关系主键，查询已经汇总出的月列表明细
     * 
     * @param
     *
     * 
     * @author liaozhanggen 2016年12月29日
     */
    public List<WorkerSalaryMonth> queryWorkerSalaryMonths(Map<String, String> param,PageDTO pageDTO){
    	PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
    	return workerSalaryMonthMapper.queryWorkerSalaryMonths(param);
    }
    
    /**
     * 查询出指定的班组及工人的每月小计
     * 
     * @author liaozhanggen 2016年12月29日
     */
    public Page<Map> queryEveryMonthsByRelationId(Map<String, Object> param,PageDTO pageDTO){
    	PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
    	return workerSalaryMonthMapper.queryEveryMonthsByRelationId(param);
    }
	
    /**
     * 11.28 dzq 根据关系查找用户某时间段内应收款，以收款，工总数
     * @param map
     * @return
     */
    public Map<String, Object> selectCountByRelation(Map<String, Object> map){
    	return workerSalaryMonthMapper.selectCountByRelation(map);
    };
    /** 
     * @Description：公司-项目-班组，统计总应付，已付，未付
     * @Author：yuxin
     * @Create Date：2017/2/17/0017
     * @param：
     * @return：
     */
    public WorkerSalaryStatisticsVO workerSalaryStatistics(String userCode,Integer workerItemsRelationId){
		Map map = new HashMap();
		map.put("userCode", userCode);
		map.put("workerItemsRelationId", workerItemsRelationId);
		//通过realtion和usercode查询公司-项目-班组
		Map teamProjectCompany = workerSalaryMonthMapper.selectTeamProjectCompany(map);
		//通过realtion和usercode查询应付已付未付 yuxin
		Map money = workerSalaryMonthMapper.selectMoney(map);
		WorkerSalaryStatisticsVO wssvo = new WorkerSalaryStatisticsVO();
		if (teamProjectCompany != null) {
			wssvo.setCompanyName((String) teamProjectCompany.get("company_name"));
			wssvo.setProjectName((String) teamProjectCompany.get("project_name"));
			wssvo.setTeamName((String) teamProjectCompany.get("user_name"));
		}
		if (money != null) {
			wssvo.setReceivableMoney((BigDecimal) money.get("receivableSum"));
			wssvo.setReceivedMoney((BigDecimal) money.get("receivedSum"));
			wssvo.setUnpaidMoney(wssvo.getReceivableMoney().subtract(wssvo.getReceivedMoney()));
		}
		return wssvo;
	}
    
    /**
     * 11.29 dzq 根据班组id查找用户某时间段内应收款，以收款，工总数、班组总成员人数
     * 		update code liaozhanggen 2016-12-30
     * @param map
     * @return
     */
    public Map<String, Object> selectWorkerAmountSumByTeamId(Map<String, Object> map){
    	return workerSalaryMonthMapper.selectWorkerAmountSumByTeamId(map);
    }
    
    /**
     * 11.30 dzq 工人工资月表，根据teamId查找其下所有工人工资信息
     * 		update code liaozhanggen 2016-12-30
     * @param map
     * @return
     */
    public Page<WorkerSalaryMonthVO> selectMemberByTeamId(Map<String, Object> map,PageDTO pageDTO){
    	PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
    	return workerSalaryMonthMapper.selectWorkerMonthByTeamId(map);
    }

	
	/**
	 * 根据考勤日，每位员工、所在的班组、统计月考勤（统计之后 ，修改数据，得不到统计）
	 * 
	 * @auth liaozhanggen 2016-11-29 下午3:40:01
	 */
	public String saveWorkerSalaryMonthCalc(Map<String, String> param){
		return workerSalaryMonthMapper.countWorkAttendanceCalc(param);
	}

	/**
	 * 根据考勤日，每位员工、所在的班组、统计(可实时统计当月当前职员的月考勤统计信息包括变动)考勤
	 * 
	 * @param param message
	 * 				userCode
	 * 				currentDate
	 * @auth liaozhanggen add  code 2016-11-30 下午2:09:00
	 * @auth liaozhanggen update code 2017-02-25 下午16:40:00
	 */
	public String saveWorkerSalaryCurrentMonthCalc(Map<String, String> param) {
		return workerSalaryMonthMapper.countWorkAttendanceCurrentMonthCalc(param);
	}
}
